package com.rxy.sqlSession;

import com.rxy.pojo.Configuration;
import com.rxy.pojo.MapperStatement;

import java.lang.reflect.*;
import java.sql.SQLException;
import java.util.List;

public class DefaultSqlSession implements SqlSession {

    private Configuration configuration;

    public DefaultSqlSession(Configuration configuration) {
        this.configuration = configuration;
    }

    private SimpleExecutor simpleExecutor = new SimpleExecutor();

    @Override
    public <E> List<E> selectList(String statementId, Object... param) throws Exception {
        MapperStatement mapperStatement = configuration.getMapperStatementMap().get(statementId);
        List<Object> list = simpleExecutor.query(configuration, mapperStatement, param);
        return (List<E>) list;
    }

    @Override
    public <T> T selectOne(String statementId, Object... param) throws Exception {
        List<Object> objects = selectList(statementId, param);
        if(objects.size() == 1){
            return (T) objects.get(0);
        }else{
            throw new RuntimeException("查询结果为空或者返回结果过多");
        }
    }

    @Override
    public int update(String statementId, Object... param) throws Exception {
        MapperStatement mapperStatement = configuration.getMapperStatementMap().get(statementId);
        return simpleExecutor.update(configuration, mapperStatement, param);
    }

    @Override
    public int delete(String statementId, Object... param) throws Exception {
        return update(statementId, param);
    }

    @Override
    public void close() throws SQLException {
        simpleExecutor.close();
    }

    @Override
    public <T> T getMapper(Class<?> mapperClass) {
        //使用jdk动态代理为Dao接口生成代理对象
        Object proxyInstance = Proxy.newProxyInstance(DefaultSqlSession.class.getClassLoader(), new Class[]{mapperClass}, new InvocationHandler() {
            @Override
            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                //底层还是去执行jdbc代码，根据不同情况，来调用selectList或者selectOne
                //准备参数1：statementId： namespace.id = 接口全限定名.方法名
                String name = method.getName(); //方法名
                String className = method.getDeclaringClass().getName(); //类名
                String statementId = className + "." + name;
                //准备参数2：params = args
                String sqlType = configuration.getMapperStatementMap().get(statementId).getSqlType();
                switch (sqlType){
                    case "select":
                        //获取被调用方法的返回值类型
                        Type genericReturnType = method.getGenericReturnType();
                        //简单判断：是否进行了泛型类型参数化
                        if(genericReturnType instanceof ParameterizedType){
                            return selectList(statementId, args);
                        }
                        return selectOne(statementId, args);
                    case "update":
                        return update(statementId, args);
                    case "delete":
                        return delete(statementId, args);
                    default:
                        return null;
                }
            }
        });
        return (T) proxyInstance;
    }
}
